﻿#include "subdataprotocoleditor.h"
#include "ui_subdataprotocoleditor.h"

SubDataProtocolEditor::SubDataProtocolEditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SubDataProtocolEditor)
{
    ui->setupUi(this);
    ui->TypeBox->setView(new QListView());
    nEditorType =ADD_DATA;
}

SubDataProtocolEditor::~SubDataProtocolEditor()
{
    delete ui;
}

void SubDataProtocolEditor::InitSubDataProtocolEditor(int nType,int nMsgType ,const QString sInfo ,const QString sMsg)
{
    ui->InfoEdit->clear();
    ui->textEdit->clear();
    QString sTitleName = "";
    QString sIconPath = "";
    nEditorType =nType;
    switch (nType)
    {
    case ADD_DATA:
    {
        sTitleName = "添加数据";
        sIconPath =":/Icon/Add.png";
        break;
    }
    case EDIT_DATA:
    {
        sTitleName = "编辑数据";
        sIconPath =":/Icon/Edit.png";

        ui->InfoEdit->setText(sInfo);
        ui->textEdit->setText(sMsg);
        ui->TypeBox->setCurrentIndex(nMsgType);

        break;
    }
    default:
        break;
    }
    ui->TypeBox->setStyleSheet("QComboBox QAbstractItemView::item{height:30px;}"  );//下拉选项高度
    setWindowIcon(QIcon(sIconPath));
    setWindowTitle(sTitleName);
    setFixedSize(400,500);
}

void SubDataProtocolEditor::on_QuitBtn_clicked()
{
    ui->InfoEdit->clear();
    ui->textEdit->clear();
    close();
}

void SubDataProtocolEditor::on_EnterBtn_clicked()
{
    int nType =  ui->TypeBox->currentIndex();
    QString sData = ui->InfoEdit->text();
    QString sInfoData =  ui->textEdit->toPlainText();

    if(sData.isEmpty())
    {
        QMessageBox::critical(this, "错误", "数据描述不能为空!","确定");
        return;
    }

    close();
    if(nEditorType == ADD_DATA)
    {
        emit on_CreateSubDataProtocol(nType,sData,sInfoData);
    }else
    {
        emit on_EditorSubDataProtocol(nType,sData,sInfoData);
    }
}
